import Vue from "vue";

//配置路由相关信息
import VueRouter from "vue-router";

import store from "../store/index";

import PwdChange from "../views/home/PwdChange";
import companyUserLogin from "../views/home/Login";
import LogoutUser from "../views/home/LogoutUser";
import PersonInfo from "@/views/home/PersonInfo";
import CompanyInfo from "@/views/company/CompanyInfo";
import ResumeManage from "@/views/company/ResumeManage";
import RecruitManage from "@/views/company/RecruitManage";
import TalentsSearch from "@/views/company/TalentsSearch";
import PreachManage from "@/views/company/PreachManage";
import JobManage from "@/views/company/JobManage";
import Live from "@/components/content/Live";


//1.通过Vue.use(插件),安装插件
Vue.use(VueRouter);

//2.创建路由对象
const routes = [
    {
        path: "",
        redirect: "/login",
    },
    {
        path: '/loading',
        component: () => import("../components/common/Loading")

    },

    {
        path: "/home",
        component: () => import("../views/home/Home"),
        meta: {
            title: "首页"
        },
        children: [
            {
                path: "",
                name: "companyInfo",
                component: CompanyInfo,
                meta: {
                    title: "企业信息",
                    requireAuth: true,
                },
            },

            {
                path: 'jobList',
                component: () => import("../views/company/JobManage"),
                meta: {
                    title: '职位管理',
                    requireAuth: true
                },
            },
            {
                path: 'videoList',
                component: () => import("../views/company/VideoManage"),
                meta: {
                    title: '宣讲视频管理',
                    requireAuth: true
                },
            },
            {
                path: 'liveList',
                component: () => import("../views/company/LiveManage"),
                meta: {
                    title: '宣讲直播管理',
                    requireAuth: true
                },
            },
            {
                path: "pwdChange",
                component: PwdChange,
                meta: {
                    title: "修改密码",
                    requireAuth: true,
                },
            },
            {
                path: "logoutUser",
                component: LogoutUser,
                meta: {
                    title: "账号注销",
                    requireAuth: true,
                },
            },
            {
                path: "personInfo",
                name: "personInfo",
                component: PersonInfo,
                meta: {
                    title: "个人信息",
                    requireAuth: true,
                },
            },
            {
                path: "jobManage",
                name: "jobManage",
                component: JobManage,
                meta: {
                    title: "职位管理",
                    requireAuth: true,
                },
            },
            {
                path: "resumeManage",
                name: "resumeManage",
                component: ResumeManage,
                meta: {
                    title: "接收简历管理",
                    requireAuth: true,
                },
            },
            {
                path: "recruitManage",
                name: "recruitManage",
                component: RecruitManage,
                meta: {
                    title: "招聘管理",
                    requireAuth: true,
                },
            },
            {
                path: "talentsSearch",
                name: "talentsSearch",
                component: TalentsSearch,
                meta: {
                    title: "人才搜索",
                    requireAuth: true,
                },
            },
            {
                path: "preachManage",
                name: "preachManage",
                component: PreachManage,
                meta: {
                    title: "宣讲管理",
                    requireAuth: true,
                },
            },
            {
                path: "live",
                name: "live",
                component: Live,
                meta: {
                    title: "直播",
                    requireAuth: true,
                },
            },
        ]
    },
    {
        path: "/login",
        component: companyUserLogin,
        meta: {
            title: "登录",
        },
    },
    {
        path: "/companyAuth",
        name: "companyAuth",
        component: () => import("../views/home/CompanyAuth"),
        meta: {
            title: "企业认证",
        },
    },
    {
        path: "/rePwd",
        name: "rePwd",
        component: () => import("../views/home/RePwd"),
        meta: {
            title: "忘记密码",
        },
    },
];

const router = new VueRouter({
    //配置路由和组件之间的关系
    routes,
    // mode: "history",
    linkActiveClass: "active",
});

// 前置守卫(guard)
router.beforeEach((to, from, next) => {
    if (to.matched.some((r) => r.meta.requireAuth)) {
        //这里的requireAuth为路由中定义的 meta:{requireAuth:true}，意思为：该路由添加该字段，表示进入该路由需要登陆的
        if (store.getters.token) {
            next();
            let matched = to.matched
            console.log(to.matched);
            let list = [];
            matched.forEach(element => {
                list.push({"title": element.meta.title, "path": element.path})
            });
            store.dispatch("user/setBreads", list)
        } else {
            next({
                path: "/login",
            });
        }
    } else {
        next();
    }
});


//后置钩子(hook)
router.afterEach((route) => {
    console.log(route.fullPath)
});

//解决两次访问相同路由报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};

// 3.将router对象传到vue实例
export default router;

